﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;


namespace CARMS.Controls
{
    public partial class MessageControl : UserControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadMessage();
            }
        }


        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (G100Entity) e.Row.DataItem;

                var linkPickUp = e.Row.FindControl("linkPickUp") as HyperLink;
                var reportHeader = ReportBusiness.GetReportHeaderEntityByReportNumber(entity.G1001);
                if(reportHeader!=null)
                {
                    linkPickUp.NavigateUrl = SiteUrls.Report.GenerateReport + "?ReportHeaderID=" + reportHeader.ReportHeaderId + "&ReportCategoryID="+reportHeader.ReportCategoryId;
                }
                else
                {
                    linkPickUp.Visible = false;
                }
            }
        }

        protected void gridList_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {

                case "ViewItem":
                    RediretResolveUrl(SiteUrls.Message.MessageDetail + "?GID=" + e.CommandArgument.ToString());
                    break;
                case "DeleteItem":
                    //var boolSuccess = MessageBusiness.InActiveMessage(new Guid(e.CommandArgument.ToString()));
                    //if (boolSuccess)
                    //{
                    //    LoadMessage();
                    //}
                    //else
                    //{
                    //    base.Alert("Delete Failed.");
                    //}
                    break;
                //case "PickUp":
                //    var msgID =int.Parse(e.CommandArgument.ToString());
                //    var message = new MessageEntity(msgID);
                //    if (message.ActionUrl.Trim().Length > 0)
                //    {
                //        RediretResolveUrl(message.ActionUrl);
                //    }
                //    else
                //    {
                //        RediretResolveUrl(SiteUrls.Message.MessageDetail + "?MessageID=" + msgID);
                //    }
                //    break;
                    
            }
        }

        private void LoadMessage()
        {

            //var collection = new MessageCollection();
            //collection.GetMulti(MessageFields.TargetUserId == CurrentControlLoginUser.UserID & MessageFields.IsActive == true, 10);
            //gridList.DataSource = collection;
            //gridList.DataBind();

            var collection = new G100Collection();
            collection.GetMulti(G100Fields.G1005 == string.Empty |G100Fields.G1005 %("%"+CurrentControlLoginUser.LoginName+"%") , 10);
            gridList.DataSource = collection;
            gridList.DataBind();
 
        }
    }
}